﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using UnityEngine;

public class CSVTable
{
    [CompilerGenerated]
    private static Func<string, bool> <>f__am$cache3;
    [CompilerGenerated]
    private static Func<Element, bool> <>f__am$cache4;
    private Dictionary<string, Element> elementLookup = new Dictionary<string, Element>();
    public List<Element> elements = new List<Element>();
    public string[] headers;

    public CSVTable(string table)
    {
        string[] separator = new string[] { Environment.NewLine };
        if (<>f__am$cache3 == null)
        {
            <>f__am$cache3 = new Func<string, bool>(CSVTable.<CSVTable>m__192);
        }
        string[] source = table.Split(separator, StringSplitOptions.RemoveEmptyEntries).Where<string>(<>f__am$cache3).ToArray<string>();
        char[] chArray1 = new char[] { ',' };
        this.headers = source[0].Split(chArray1);
        if (<>f__am$cache4 == null)
        {
            <>f__am$cache4 = new Func<Element, bool>(CSVTable.<CSVTable>m__194);
        }
        this.elements = source.ToList<string>().GetRange(1, source.Length - 1).Select<string, Element>(new Func<string, Element>(this.<CSVTable>m__193)).Where<Element>(<>f__am$cache4).ToList<Element>();
        foreach (Element element in this.elements)
        {
            string header = this.headers[0];
            string key = element.GetString(header);
            if (!this.elementLookup.ContainsKey(key))
            {
                this.elementLookup.Add(key, element);
            }
        }
    }

    [CompilerGenerated]
    private static bool <CSVTable>m__192(string x)
    {
        return !x.StartsWith("//");
    }

    [CompilerGenerated]
    private Element <CSVTable>m__193(string x)
    {
        return new Element(x, this.headers);
    }

    [CompilerGenerated]
    private static bool <CSVTable>m__194(Element x)
    {
        return !string.IsNullOrEmpty(x.key);
    }

    public bool ContainsElement(string key)
    {
        return this.elementLookup.ContainsKey(key);
    }

    public Element GetElement(string key)
    {
        return (!this.ContainsElement(key) ? null : this.elementLookup[key]);
    }

    [DebuggerHidden]
    public IEnumerable<Element> GetElements(string key)
    {
        return new <GetElements>c__Iterator2C { key = key, <$>key = key, <>f__this = this, $PC = -2 };
    }

    public string firstHeader
    {
        get
        {
            return this.headers.FirstOrDefault<string>();
        }
    }

    [CompilerGenerated]
    private sealed class <GetElements>c__Iterator2C : IDisposable, IEnumerator, IEnumerable, IEnumerable<CSVTable.Element>, IEnumerator<CSVTable.Element>
    {
        internal CSVTable.Element $current;
        internal int $PC;
        internal string <$>key;
        internal List<CSVTable.Element>.Enumerator <$s_148>__0;
        internal CSVTable <>f__this;
        internal CSVTable.Element <row>__1;
        internal string key;

        [DebuggerHidden]
        public void Dispose()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 1:
                    try
                    {
                    }
                    finally
                    {
                        this.<$s_148>__0.Dispose();
                    }
                    break;
            }
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            bool flag = false;
            switch (num)
            {
                case 0:
                    this.<$s_148>__0 = this.<>f__this.elements.GetEnumerator();
                    num = 0xfffffffd;
                    break;

                case 1:
                    break;

                default:
                    goto Label_00CF;
            }
            try
            {
                while (this.<$s_148>__0.MoveNext())
                {
                    this.<row>__1 = this.<$s_148>__0.Current;
                    if (this.<row>__1.GetString(this.<>f__this.firstHeader) == this.key)
                    {
                        this.$current = this.<row>__1;
                        this.$PC = 1;
                        flag = true;
                        return true;
                    }
                }
            }
            finally
            {
                if (!flag)
                {
                }
                this.<$s_148>__0.Dispose();
            }
            this.$PC = -1;
        Label_00CF:
            return false;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        [DebuggerHidden]
        IEnumerator<CSVTable.Element> IEnumerable<CSVTable.Element>.GetEnumerator()
        {
            if (Interlocked.CompareExchange(ref this.$PC, 0, -2) == -2)
            {
                return this;
            }
            return new CSVTable.<GetElements>c__Iterator2C { <>f__this = this.<>f__this, key = this.<$>key };
        }

        [DebuggerHidden]
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.System.Collections.Generic.IEnumerable<CSVTable.Element>.GetEnumerator();
        }

        CSVTable.Element IEnumerator<CSVTable.Element>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }

    public class Element
    {
        public Dictionary<string, string> lookup = new Dictionary<string, string>();

        public Element(string rawLine, string[] headers)
        {
            string[] source = Regex.Split(rawLine, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
            for (int i = 0; i < headers.Length; i++)
            {
                string str = source[i];
                if (str.StartsWith("\"") && str.EndsWith("\""))
                {
                    char[] trimChars = new char[] { '"' };
                    str = str.Trim(trimChars);
                }
                if (!string.IsNullOrEmpty(headers[i]) && !this.lookup.ContainsKey(headers[i]))
                {
                    this.lookup.Add(headers[i], str);
                }
            }
            this.key = source.FirstOrDefault<string>();
        }

        private T Convert<T>(object value)
        {
            try
            {
                return (T) System.Convert.ChangeType(value, typeof(T));
            }
            catch (Exception)
            {
            }
            return default(T);
        }

        public bool GetBool(string header)
        {
            return this.GetValue<bool>(header);
        }

        public float GetFloat(string header)
        {
            return this.GetValue<float>(header);
        }

        public int GetInt(string header)
        {
            return this.GetValue<int>(header);
        }

        public string GetString(string header)
        {
            return this.GetValue(header);
        }

        private string GetValue(string header)
        {
            if (this.lookup.ContainsKey(header))
            {
                return this.lookup[header];
            }
            return string.Empty;
        }

        public T GetValue<T>(string header)
        {
            return this.Convert<T>(this.GetValue(header));
        }

        public Vector3 GetVector(string header)
        {
            char[] separator = new char[] { ',' };
            string[] strArray = this.GetString(header).Split(separator);
            if (strArray.Length == 2)
            {
                return (Vector3) new Vector2(this.Convert<float>(strArray[0]), this.Convert<float>(strArray[1]));
            }
            if (strArray.Length == 3)
            {
                return new Vector3(this.Convert<float>(strArray[0]), this.Convert<float>(strArray[1]), this.Convert<float>(strArray[2]));
            }
            return Vector3.zero;
        }

        public string key { get; private set; }
    }
}

